package com.binarysoft.service.obj.dao;

import java.util.Collection;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;

public interface ObjectEntityDAO extends ObjectEntityMapper {

	@Select({ "select", "obj_id, obj_name, obj_desc, obj_addr, parent_obj_id, root_obj_id, obj_hierarchy, ", "obj_index, obj_type_id, is_active, creation_time, last_updated_time",
			"from object_entity" })
	@Results({ @Result(column = "obj_id", property = "objId", jdbcType = JdbcType.INTEGER, id = true), @Result(column = "obj_name", property = "objName", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_desc", property = "objDesc", jdbcType = JdbcType.VARCHAR), @Result(column = "obj_addr", property = "objAddr", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent_obj_id", property = "parentObjId", jdbcType = JdbcType.INTEGER), @Result(column = "root_obj_id", property = "rootObjId", jdbcType = JdbcType.INTEGER),
			@Result(column = "obj_hierarchy", property = "objHierarchy", jdbcType = JdbcType.INTEGER), @Result(column = "obj_index", property = "objIndex", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_type_id", property = "objTypeId", jdbcType = JdbcType.VARCHAR), @Result(column = "is_active", property = "isActive", jdbcType = JdbcType.BIT),
			@Result(column = "creation_time", property = "creationTime", jdbcType = JdbcType.DATE), @Result(column = "last_updated_time", property = "lastUpdatedTime", jdbcType = JdbcType.DATE) })
	Collection<ObjectEntity> selectAll();

	@Select({ "select", "obj_id, obj_name, obj_desc, obj_addr, parent_obj_id, root_obj_id, obj_hierarchy, ", "obj_index, obj_type_id, is_active, creation_time, last_updated_time",
			"from object_entity", "where obj_id = root_obj_id" })
	@Results({ @Result(column = "obj_id", property = "objId", jdbcType = JdbcType.INTEGER, id = true), @Result(column = "obj_name", property = "objName", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_desc", property = "objDesc", jdbcType = JdbcType.VARCHAR), @Result(column = "obj_addr", property = "objAddr", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent_obj_id", property = "parentObjId", jdbcType = JdbcType.INTEGER), @Result(column = "root_obj_id", property = "rootObjId", jdbcType = JdbcType.INTEGER),
			@Result(column = "obj_hierarchy", property = "objHierarchy", jdbcType = JdbcType.INTEGER), @Result(column = "obj_index", property = "objIndex", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_type_id", property = "objTypeId", jdbcType = JdbcType.VARCHAR), @Result(column = "is_active", property = "isActive", jdbcType = JdbcType.BIT),
			@Result(column = "creation_time", property = "creationTime", jdbcType = JdbcType.DATE), @Result(column = "last_updated_time", property = "lastUpdatedTime", jdbcType = JdbcType.DATE) })
	Collection<ObjectEntity> selectAllRootObjs();

	@Select({ "select", "obj_id, obj_name, obj_desc, obj_addr, parent_obj_id, root_obj_id, obj_hierarchy, ", "obj_index, obj_type_id, is_active, creation_time, last_updated_time",
			"from object_entity", "where obj_id = root_obj_id and is_active = 1" })
	@Results({ @Result(column = "obj_id", property = "objId", jdbcType = JdbcType.INTEGER, id = true), @Result(column = "obj_name", property = "objName", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_desc", property = "objDesc", jdbcType = JdbcType.VARCHAR), @Result(column = "obj_addr", property = "objAddr", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent_obj_id", property = "parentObjId", jdbcType = JdbcType.INTEGER), @Result(column = "root_obj_id", property = "rootObjId", jdbcType = JdbcType.INTEGER),
			@Result(column = "obj_hierarchy", property = "objHierarchy", jdbcType = JdbcType.INTEGER), @Result(column = "obj_index", property = "objIndex", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_type_id", property = "objTypeId", jdbcType = JdbcType.VARCHAR), @Result(column = "is_active", property = "isActive", jdbcType = JdbcType.BIT),
			@Result(column = "creation_time", property = "creationTime", jdbcType = JdbcType.DATE), @Result(column = "last_updated_time", property = "lastUpdatedTime", jdbcType = JdbcType.DATE) })
	Collection<ObjectEntity> selectAllActiveRootObjs();

	@Select({ "select", "obj_id", "from object_entity", "where obj_id = root_obj_id" })
	@Results({ @Result(column = "obj_id", property = "objId", jdbcType = JdbcType.INTEGER, id = true) })
	Collection<Long> selectAllRootObjIDs();

	@Select("<script>" + "select " 
			+ " obj_id, obj_name, obj_desc, obj_addr, parent_obj_id, root_obj_id, obj_hierarchy, " 
			+ " obj_index, obj_type_id, is_active, creation_time, last_updated_time "
			+ " from object_entity where obj_id in " 
			+ "<foreach item='item' index='index' collection='idList' open='(' separator=',' close=')'>" + "#{item}" + "</foreach>" 
			+ "</script>")
	@Results({ @Result(column = "obj_id", property = "objId", jdbcType = JdbcType.INTEGER, id = true), @Result(column = "obj_name", property = "objName", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_desc", property = "objDesc", jdbcType = JdbcType.VARCHAR), @Result(column = "obj_addr", property = "objAddr", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent_obj_id", property = "parentObjId", jdbcType = JdbcType.INTEGER), @Result(column = "root_obj_id", property = "rootObjId", jdbcType = JdbcType.INTEGER),
			@Result(column = "obj_hierarchy", property = "objHierarchy", jdbcType = JdbcType.INTEGER), @Result(column = "obj_index", property = "objIndex", jdbcType = JdbcType.VARCHAR),
			@Result(column = "obj_type_id", property = "objTypeId", jdbcType = JdbcType.VARCHAR), @Result(column = "is_active", property = "isActive", jdbcType = JdbcType.BIT),
			@Result(column = "creation_time", property = "creationTime", jdbcType = JdbcType.DATE), @Result(column = "last_updated_time", property = "lastUpdatedTime", jdbcType = JdbcType.DATE) })
	public Collection<ObjectEntity> getObjsByIds(@Param("idList") Collection<Long> idList);
}
